--
-- Author: shunguo.chen
-- Date: 2017-11-16 16:46:34
--
-- BoxRender

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

-- data = BoxItemVo
function ClassRef.create(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end       

function ClassRef:init(data)
    self.super.init(self, data)

    self._render = IconRender.create(self._ui.icon)
    self._render:setTipUnShowOrigin(true)
    self._render:setTipEnable(true)
    self._render:setClickEnable(false)

    -- 显示tip位置
    local size = self:getContentSize()
    local posX = self:getPositionX()
    local posY = self:getPositionY()

    local pos = {x=0, y=posY/640}
    if posX <= display.cx then
        pos.x = (posX + size.width + 50)/display.ui_width
    else
        pos.x = (posX - size.width - 50)/display.ui_width
    end

    display.uiAddClick(self, function()
            self._render:showTip(pos)
            executeFunc(self._onBtnClick)
        end)
end

function ClassRef:refresh()
    if self._data then
        local itemData = self._data.itemData
        if itemData then
            self._render:setData(itemData)
            self._ui.type:setString(itemData:getTypeName())
            self._ui.name:setColor(Enums.QualityColor[self._data.quality])
            -- 如果是货币，不显示名字，要显示数量
            if itemData.itemBagType == Enums.ItemBagType.daibi then
                self._render:setNumBgVisible(false)
                self._ui.name:setString(itemData.amount)
            else
                self._ui.name:setString(itemData:getName())
            end
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onBtnClick = cb
end

return ClassRef